package controller

import (
	"smc-admin/models"
	"smc-admin/response"
	"smc-admin/utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Video struct {
	Name     string `form:"name" binding:"required"`
	TypesId  int    `form:"typesId" binding:"required"`
	FileList string `form:"fileList"`
}

type VideoById struct {
	ID int `form:"id" binding:"required"`
	Video
}

// @Tags         视频管理
// @Summary      视频更新
// @Param        video  body	Video  true  "视频"
// @Success      200       {object}  response.Body
// @Router       /video/update [PUT]
func UpdateVideo(ctx *gin.Context) {
	var video VideoById
	err := ctx.ShouldBindJSON(&video)
	if err != nil {
		panic(err.Error())
	}

	//更新
	i := models.Video{
		BaseModel: models.BaseModel{
			ID: video.ID,
		},
	}
	updateErr := i.Update(map[string]interface{}{
		"name":      video.Name,
		"types_id":  video.TypesId,
		"file_list": video.FileList,
	})
	if updateErr != nil {
		panic(updateErr.Error())
	}
	response.Success(ctx)
}

// @Tags         视频管理
// @Summary      视频添加
// @Param        video  body	Video  true  "视频"
// @Success      200       {object}  response.Body
// @Router       /video/add [POST]
func AddVideo(ctx *gin.Context) {
	var video Video
	error := ctx.ShouldBindJSON(&video)
	if error != nil {
		panic(error.Error())
	}

	i := models.Video{
		FileList: video.FileList,
		TypesID:  video.TypesId,
		Name:     video.Name,
	}
	e := i.Add()
	if e != nil {
		panic(e.Error())
	}

	response.Success(ctx)
}

// @Tags         视频管理
// @Summary      视频分页查询
// @Param        id	query	int  false  "id"
// @Param        name	query	string  false  "名称"
// @Param        typesId	query	int  false  "类型id"
// @Param        pageSize	query	string  false  "分页大小"
// @Param        pageNum	query	string  false  "分页数量"
// @Success      200       {object}  response.Body
// @Router       /video/findByPage [get]
func FindVideoByPage(ctx *gin.Context) {
	name := ctx.Query("name")
	id, _ := strconv.Atoi(ctx.Query("id"))
	typesId, _ := strconv.Atoi(ctx.Query("typesId"))

	i := models.Video{
		BaseModel: models.BaseModel{
			ID: id,
		},
		Name:    name,
		TypesID: typesId,
	}
	data, total, err := i.FindByPage(utils.PageQuery(ctx))
	if err != nil {
		panic(err.Error())
	}
	response.Success(ctx, response.Body{
		Data: map[string]interface{}{
			"data":  data,
			"total": total,
		},
	})
}

// @Tags         视频管理
// @Summary      视频删除
// @Param        id	query	string  false  "视频id"
// @Success      200       {object}  response.Body
// @Router       /video/delete [DELETE]
func DeleteVideo(ctx *gin.Context) {
	deleteId, _ := strconv.Atoi(ctx.Query("id"))

	i := models.Video{
		BaseModel: models.BaseModel{
			ID: deleteId,
		},
	}
	err := i.Delete()
	if err != nil {
		panic(err)
	}
	response.Success(ctx)
}
